package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.ExhibitorEmployeeDTO;
import com.eastfair.imaster.dto.ExhibitorEmployeePageQuery;
import com.eastfair.imaster.dto.ExhibitorEmployeeSaveDTO;
import com.eastfair.imaster.dto.ExhibitorEmployeeUpdateDTO;
import com.eastfair.imaster.entity.Exhibitor;
import com.eastfair.imaster.entity.ExhibitorEmployee;
import com.eastfair.imaster.service.ExhibitorEmployeeService;
import com.eastfair.imaster.service.ExhibitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 展商员工
 * </p>
 *
 * @author ligang
 * @date 2021-09-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/exhibitorEmployee")
@Api(value = "ExhibitorEmployee", tags = "展商员工")
public class ExhibitorEmployeeController extends SuperController<ExhibitorEmployeeService, Long, ExhibitorEmployee, ExhibitorEmployeePageQuery, ExhibitorEmployeeSaveDTO, ExhibitorEmployeeUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    ExhibitorService exhibitorService;
    @Override
    public void handlerResult(IPage<ExhibitorEmployee> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ExhibitorEmployee> exhibitorEmployeeList = list.stream().map((map) -> {
            ExhibitorEmployee exhibitorEmployee = ExhibitorEmployee.builder().build();
            //TODO 请在这里完成转换
            return exhibitorEmployee;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(exhibitorEmployeeList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ExhibitorEmployeePageQuery> params) {
        ExhibitorEmployeePageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }



    /**
     * 添加公司员工到展商
     * @param exhibitorEmployeeDTO 实体
     *        employeeId 员工id
     *        exhibitorId 展商id
     *        exhibitionId 展会id
     * @return
     */
    @ApiOperation(value = "添加公司员工到展商", notes = "创建节点组")
    @PostMapping("/addEmployeeOfExhibitor")
    public R addCompanyOfExhibitor(@RequestBody @Validated({ExhibitorEmployeeDTO.addCompanyOfExhibitor.class}) ExhibitorEmployeeDTO exhibitorEmployeeDTO) {
        //展商id查询公司id
        List<Exhibitor> exhibitorList = exhibitorService.list(null, Arrays.asList(exhibitorEmployeeDTO.getExhibitorId()));
        if(exhibitorList!=null && !exhibitorList.isEmpty()){
            R.fail(ExceptionCode.EXHIBITOR_NULL);
        }
        //填充公司id
        exhibitorEmployeeDTO.setCompanyId(exhibitorList.get(0).getCompanyId());
        return baseService.addEmployeeOfExhibitor(exhibitorEmployeeDTO);
    }

    /**
     * 展商剔除公司员工
     * @param exhibitorEmployeeDTO 实体
     *        employeeId 员工id
     *        exhibitorId 展商id
     *        companyId 公司id
     *        exhibitionId 展会id
     * @return
     */
    @ApiOperation(value = "展商剔除公司员工", notes = "创建节点组")
    @PostMapping("/removeEmployeeOfExhibitor")
    public R removeEmployeeOfExhibitor(@RequestBody @Validated({ExhibitorEmployeeDTO.removeEmployeeOfExhibitor.class}) ExhibitorEmployeeDTO exhibitorEmployeeDTO) {
        return baseService.removeEmployeeOfExhibitor(exhibitorEmployeeDTO.getId());
    }


}
